package com.gimi.cloud.bbp.manage.service.product.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.product.LogisticsCompanyDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.product.LogisticsCompanyDTO;
import com.gimi.cloud.bbp.entity.product.LogisticsCompanyEntity;
import com.gimi.cloud.bbp.manage.service.mall.impl.MallAppBannerServiceImpl;
import com.gimi.cloud.bbp.manage.service.product.LogisticsCompanyService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* 物流公司管理相关接口实现类
* @author zhoufuhang
* @Description
* @create 2018-02-01
* @version 1.0
**/
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class LogisticsCompanyServiceImpl implements LogisticsCompanyService{

	    private static final Logger logger = LoggerFactory.getLogger(MallAppBannerServiceImpl.class);
	    @Autowired private LogisticsCompanyDao logisticsCompanyDao;


	    @Override
	    public List<LogisticsCompanyDTO> queryList(LogisticsCompanyDTO dto, boolean isPaging) throws Exception {
	        if (isPaging){
	            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
	        }
	        String sql = "SELECT * FROM ec_logistics_company WHERE is_delete = 0 and company_name=:companyName AND company_tel=:companyTel";
	        QueryParam param = new QueryParam();
	        param.setSql(sql);
	        param.setParamObject(dto);
	        return logisticsCompanyDao.selectListDTOBySql(param);
	    }

	    @Override
	    public LogisticsCompanyDTO load(LogisticsCompanyDTO dto, Object id) throws Exception {
	        LogisticsCompanyEntity entity = logisticsCompanyDao.queryBy("lc_id",id);
	        if(null == entity){
	            return null;
	        }
	        LogisticsCompanyDTO resultDTO = new LogisticsCompanyDTO();
	        ReflectUtils.copyPropertiesIgnoreNull(entity,resultDTO);
	        return resultDTO;
	    }

	    @Override
	    public LogisticsCompanyDTO saveOrUpdate(LogisticsCompanyDTO dto) throws Exception {
	        LogisticsCompanyEntity entity = null;
	        if (null != dto.getLcId()) {
	            entity = logisticsCompanyDao.queryBy("lc_id", dto.getLcId());
	            if (null == entity) {
	                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取id为" + dto.getLcId() + "的信息，更新失败");
	            }
	        } else {
	            entity = new LogisticsCompanyEntity();
	        }
	        ReflectUtils.copyPropertiesIgnoreNull(dto, entity);
	        if (null != dto.getLcId()) {
	        	logisticsCompanyDao.baseUpdateByPrimaryKey(entity);
	        } else {
	        	logisticsCompanyDao.baseInsert(entity);
	        }
	        ReflectUtils.copyPropertiesIgnoreNull(entity, dto);
	        return dto;
	    }

	    @Override
	    public int delete(LogisticsCompanyDTO dto, Object id, boolean realDel) throws Exception {
	        LogisticsCompanyEntity entity = logisticsCompanyDao.queryBy("lc_id", id);
	        if (null == entity) {
	            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取id为" + id + "的信息，删除失败");
	        }
	        //非物理删除
	        if(!realDel){
	            ReflectUtils.copyPropertiesIgnoreNull(dto,entity);
	            entity.setIsDelete(1);
	            return logisticsCompanyDao.baseUpdateByPrimaryKey(entity);
	        }
	        return logisticsCompanyDao.deleteByPrimaryKey(id);
	    }

}
